﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class ManufacturerAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //设置经办人
               Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j => j.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
               // UART.BLL.Sales.Manufacturer bll = new UART.BLL.Sales.Manufacturer();
                Sales_Manufacturer m = new Sales_Manufacturer();
                m.Address = mk.InputText(this.txtAddress.Text, 100);
                m.CellPhone = mk.InputText(this.txtCellPhone.Text, 20);
                m.ContactPersonName = mk.InputText(this.txtContactPersonName.Text, 20);
                m.ManufacturerName = mk.InputText(this.txtManufacturerName.Text, 50);
                m.Fax = mk.InputText(this.txtFax.Text, 20);
                m.ModifiedDate = System.DateTime.Now;
                m.Notes = mk.InputText(this.txtNotes.Text, 200);
                m.Phone = mk.InputText(this.txtPhone.Text, 20);
                m.BankAccount = Server.HtmlEncode(mk.InputText(this.txtBankAccount.Text, 200));
                m.RootDepartmentID = int.Parse(this.lblRootDepartmentID.Text);
              //  m.ManufacturerCode = mk.InputText(this.txtManufacturerCode.Text, 20);
                try
                {
                    mk.l.Sales_Manufacturer.InsertOnSubmit(m);
                    mk.l.SubmitChanges();
                    int ManufacturerID = m.ManufacturerID;
                    //添加厂家代码记录
                    string[] arrCode = mk.InputText(this.txtManufacturerCode.Text, 500).Split(',');
                   var bllCode = mk.l.Sales_ManufacturerCode.ToList();
           Sales_ManufacturerCode mCode = new Sales_ManufacturerCode();
                    if (arrCode.Length > 0)
                    {
                        for (int i = 0; i < arrCode.Length; i++)
                        {
                            if (bllCode.Where(y=>y.Code.ToString()==arrCode[i].ToString()).Count()==0)
                            {
                                mCode.ManufacturerID = ManufacturerID;
                                mCode.Code = arrCode[i].ToString();
                             //   bllCode.Add(mCode);
                                mk.l.Sales_ManufacturerCode.InsertOnSubmit(mCode);
                                mk.l.SubmitChanges();
                            }
                        }
                    }
                    mk.Alert("添加成功！", "ManufacturerList.aspx");
                }
                catch (Exception ex)
                {
                    mk.Alert("添加失败！错误信息：" + ex.Message, "ManufacturerAdd.aspx");
                }
            }
        }

    }
}